from django.http import HttpResponse, JsonResponse
from django.shortcuts import render,redirect
from mydjango.models import Users
from mydjango.models import *
from blog.models import Goods
import math


#class Tests:
    #name = ''
    #age = 0

def hw(request):
    return HttpResponse('<h1 align="center" style="color:red;">你好！</h1>')

def rd(request):
    name = request.GET.get('name')
    name = "杨平花"
    score = 59
    shops = Goods.objects.all()
    return render(request, 'index.html', {"name": name, "score": score, "shops": shops})
    #keywords = request.GET.get('keywords')
    # if keywords:
    #     shops = Goods.objects.filter(name__contains=keywords)  # 输入关键词
    # else:
    #     shops = Goods.objects.all()
    # # 接收前端传递的页码参数
    # page = request.GET.get('page')
    # # 截取数据库查询的结果的 前5个 ：利用python中的列表切片
    # shops = shops[5 * (page - 1):5 * page]
    # return render(request, "index.html", {'shops': shops, "name": name, 'stuname': stuname, 'grade': grade})


def login(request):
    if request.POST:
        username = request.POST.get('username')
        userpsd = request.POST.get('userpad')
        result = Users.objects.filter(username=username)
        #[ { username:xiaoxi,userpsd:123456} ]
        if result:
            # 判断一下 当前有值的账号对应的密码和 前端发送的密码是否匹配
            if result[0].userpsd == userpsd:
                # return HttpResponse('登陆成功！')
                return redirect('/index/?name='+result[0].username) #+:字符串拼接
            else:
                # 账号存在 密码错误
                return render(request, 'login.html')
        else:
            # 账号不存在
            # return HttpResponse("登陆失败！")
            return render(request,'register.html')
        # return render(request,"login.html")
    return render(request,'login.html')
 def ser(request):
     name = request.POST.get("name")#接收前端传递 name
     shops = Goods.objects.filter(name__contains=name)#呈现包含输入的关键词
     page = request.GET.get('page')#接收 前端传递的 页码 参数
     shops = shops[5*(page-1):5*page]# 截取数据查询的结果的 前五个 列表切片
     return render(request, 'index.html', {"shops": shops})


def register(request):
    if request.POST:
        username = request.POST.get("username")
        password = request.POST.get('password')
        #插入数据库
        #user1 = Users(uid=1,username=username,password=password)
        #数据保存
        #user1.save()
        Users.objects.create(uid=None,username=username,password=password)
    return render(request, 'register.html')

def nav(request):
    navs = [
        {
            "nav1name": "院系设置",
            "nav1url": "http://www.baidu.com",
            "nav2": [
                {
                    "nav2name": "理工农学院",
                    "nav2url": "http://www.baidu.com",
                    "nav3": [
                        {
                            "nav3name": "计算机科学与技术",
                            "nav3url": "http://www.baidu.com",
                        },
                        {
                            "nav3name": "软件工程",
                            "nav3url": "http://www.baidu.com",
                        },
                        {
                            "nav3name": "土木工程",
                            "nav3url": "http://www.baidu.com",
                        },
                        {
                            "nav3name": "园林艺术",
                            "nav3url": "http://www.baidu.com",
                        },
                    ],
                },
                {
                    "nav2name": "文艺法学院",
                    "nav2url": "http://www.baidu.com",
                },
                {
                    "nav2name": "经管学院",
                    "nav2url": "http://www.baidu.com",
                    "nav3": [
                        {
                            "nav3name": "测试1",
                            "nav3url": "http://www.baidu.com",
                        },
                        {
                            "nav3name": "测试2",
                            "nav3url": "http://www.baidu.com",
                        },
                    ],
                },
                {
                    "nav2name": "医学院",
                    "nav2url": "http://www.baidu.com",
                },
            ],
        },
        {
            "nav1name": "师资团队",
            "nav1url": "http://www.baidu.com",
            "nav2": [
                {
                    "nav2name": "111",
                    "nav2url": "http://www.baidu.com",
                }
            ],
        },
        {
            "nav1name": "科学研究",
            "nav1url": "http://www.baidu.com",
            "nav2": [
                {
                    "nav2name": "2222",
                    "nav2url": "http://www.baidu.com",
                },
                {
                    "nav2name": "2222",
                    "nav2url": "http://www.baidu.com",
                    "nav3": [
                        {
                            "nav3name": "测试333",
                            "nav3url": "http://www.baidu.com",
                        },
                        {
                            "nav3name": "测试444",
                            "nav3url": "http://www.baidu.com",
                        },
                    ],
                },
            ],
        },
        {
            "nav1name": "招生就业",
            "nav1url": "http://www.baidu.com",
            "nav2": [
                {
                    "nav2name": "333",
                    "nav2url": "http://www.baidu.com",
                },
            ],
        },
    ]
    return render(request, 'nav.html', {"navs": navs})

def navt(request):
    one = [
              {"院系设置":
                   [
                       {"理工农": ["软件工程", "计算机"]},
                       {"文艺法": ["11", "22"]}
                   ]
              },
        {"师资团队": [{"111": ["111三级","123"]}]},
        {"科学研究": [{"222": ["111三级","123"]}]},
        {"招生就业": [{"333": ["111三级","123"]}]}
    ]

    ## 测试
    two = {
        {"院系设置": {"理工农": {"专业", "专业"}}},
        {"师资团队": {"123": {"专业", "专业"}}},
        {"科学研究": {"123": {"专业", "专业"}}},
        {"招生就业": {"123": {"专业", "专业"}}}
    }
    return render(request, 'navt.html', {'one': one})